import { createRouter, createWebHistory } from "vue-router";
import Home from "../views/Home.vue";
import Course from "../views/Course.vue"
import VipPage from "../views/vippage.vue"
import Detail from "../views/Detail.vue"
import Login from "../views/Login.vue"
import Register from "../views/Register.vue"
import Courseplay from "../views/Courseplay.vue"
import ConfirmOrder from "../views/ConfirmOrder.vue"


//引入pinia
import { useUserStore } from "../store/user";

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path:"/course",
    name:"Course",
    component:Course,
  },
  {
    path:"/login",
    name:"Login",
    component:Login,
  },
  {
    path:"/register",
    name:"Register",
    component:Register,
  },
  {
    path:"/vippage",
    name:"VipPage",
    component:VipPage
  },
  {
      path:"/course/detail",
      name:"Detail",
      component:Detail
  },
  {
    path:"/courseplay/:chapterid/:courseid",
    name:"Courseplay",
    component:Courseplay
  },
  {
    path: "/cart",
    name: "Cart",
    component:() => import ("../views/Cart.vue"),
    beforeEnter:(to, from, next)=>{
      // console.log( useUserStore().token)
      if( useUserStore().token){
        next();
      }else{
        next('/login');
      }
    }
  },
  {
    path:"/confirmorder",
    name:"ConfirmOrder",
    component:ConfirmOrder
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;